﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LiteratureManage.Domain.Services
{
    using AutoMapper;
    using Castle.Services.Transaction;
    using LiteratureManage.Domain.Models;
    using LiteratureManage.Domain.DataAccess;

    [Transactional]
    public class LiteratureService : ILiteratureService 
    {
        private readonly ILiteratureDao _dao;

        public LiteratureService(ILiteratureDao dao)
        {
            this._dao = dao;
        }

        #region ILiteratureService Members

        [Transaction]
        public Literature Get(int id)
        {
            return this._dao.GetById(id, false);
        }

        [Transaction]
        public int Create(Literature model)
        {
            model.CreateDate = DateTime.Now;
            return this._dao.Create(model);
        }

        [Transaction]
        public void CreateAuthor(int literatureId, Author model)
        {
            var literature = this._dao.GetById(literatureId, false);
            model.Literature = literature;
            literature.Authors.Add(model);
        }

        [Transaction]
        public void Update(Literature model)
        {
            var persistModel = this._dao.GetById(model.Id, false);
            
            Mapper.Map<Literature, Literature>(model, persistModel);
            //persistModel.ArticleName = model.ArticleName;
            //persistModel.SubjectCatalog = model.SubjectCatalog;
            //persistModel.ArticleSource = model.ArticleSource;
            //persistModel.ArticlePublishDate = model.ArticlePublishDate;
            //this._dao.Save(persistModel);
        }

        [Transaction]
        public void UpdateAuthor(int literatureId, Author model)
        {
            var literature = this._dao.GetById(literatureId, false);
            var persistModel = (from a in literature.Authors where a.Id == model.Id select a).Single();
            Mapper.Map<Author, Author>(model, persistModel);
            //persistModel.Name = model.Name;
            //persistModel.Gender = model.Gender;
            //persistModel.Org = model.Org;
            //persistModel.Phone = model.Phone;
            //persistModel.Mobile = model.Mobile;
            //persistModel.Address = model.Address;
            //persistModel.Email = model.Email;
        }

        [Transaction]
        public void Delete(int id)
        {
            var literature = this._dao.LoadById(id, false);
            this._dao.Delete(literature);
        }

        [Transaction]
        public void DeleteAuthor(int literatureId, int id)
        {
            var literature = this._dao.LoadById(literatureId, false);
            var persistModel = (from a in literature.Authors where a.Id == id select a).SingleOrDefault();
            literature.Authors.Remove(persistModel);
        }

        [Transaction]
        public PagingResult<Literature> GetAll(int pageIndex, int pageSize)
        {
            var total = 0;
            var list = this._dao.Search(pageIndex, pageSize, null, out total);
            return new PagingResult<Literature>(list) { CurrentPage = pageIndex, PageSize = pageSize, TotalItems = total };
        }

        [Transaction(TransactionMode.RequiresNew, IsolationMode.ReadUncommitted)]
        public PagingResult<Literature> Search(int pageIndex, int pageSize, LiteratureSearchableTerm searchableTerm)
        {
            var total = 0;
            var list = this._dao.Search(pageIndex, pageSize, searchableTerm, out total);
            return new PagingResult<Literature>(list) { CurrentPage = pageIndex, PageSize = pageSize, TotalItems = total };
        }

        #endregion
    }
}
